#!/usr/bin/python3
# coding: utf-8
# authon: ren

from rest_framework import exceptions

from rest_framework.authentication import BaseAuthentication
from rest_framework.permissions import BasePermission

from mainapp.models import User, UserToken

# 登录权限验证
class MyAuthentication(BaseAuthentication):
    def authenticate(self,request):

        token = request.META.get('HTTP_AUTHORIZATION').split()[-1]
        # token = request.headers.get('Authorization',None)
        # token = request._request.GET.get('token')
        print(token)
        tk_obj = UserToken.objects.filter(token=token).first()
        if not tk_obj:
            raise exceptions.AuthenticationFailed('用户认证失败')
        return (tk_obj.user,tk_obj) # 这个元组会赋值给request,以供后续使用
    # def authenticate_header(self,request):
    #     pass
